package br.com.serctam.session;


import java.io.IOException;

import javax.ejb.Stateless;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.servlet.http.HttpServletResponse;

import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.web.RequestParameter;
import org.jboss.seam.international.StatusMessages;
import org.jboss.seam.log.Log;
import org.jboss.seam.persistence.HibernateSessionProxy;

import br.com.serctam.entity.BoletoPdf;


@Stateless
@Name("BuscaBoleto")
public class BuscaBoletoBean implements BuscaBoleto
{
    @Logger private Log log;

    @In StatusMessages statusMessages;
    
    @RequestParameter
    private Long idCandidato;
    
    @PersistenceContext
    private EntityManager entityManager;
    
    @In
    private HibernateSessionProxy hibernateSession;


    public void buscaBoleto()
    {
        // implement your business logic here
        log.info("BuscaBoleto.buscaBoleto() action called: idCandidato" + idCandidato);
        statusMessages.add("buscaBoleto");
        
        BoletoPdf boletoPdf = (BoletoPdf)entityManager.createQuery(" select b from BoletoPdf b where b.id =  " + idCandidato ).getSingleResult();
        System.out.println(boletoPdf.getBoletopdf().length);
        
        FacesContext facesContext = FacesContext.getCurrentInstance();
        HttpServletResponse response = (HttpServletResponse)facesContext.getExternalContext().getResponse();
        
        response.setContentType("application/pdf");
        response.setBufferSize(boletoPdf.getBoletopdf().length);
        response.setContentLength(boletoPdf.getBoletopdf().length);
        try {
        	response.getOutputStream().flush();
        	response.getOutputStream().write(boletoPdf.getBoletopdf());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
        
        
    }

    // add additional action methods

}
